<?php
namespace app\admin\controller;
use think\Controller;
use think\Db;
use think\Session;
use think\Request;
//角色
class Role extends Common
{	      
  public function detail(){
      $id=input('get.id');   //获取角色id
      $data=Db::table('role')->find($id);
      $zilan=json_decode($data['zilan'],true);
      $lan=[];
      foreach ($zilan as $key => $value) {
        $pid=$key;
        $parent=Db::table('rule')->find($pid);
        // dump($value);
        foreach ($value as $k => $v) {
          $zi=Db::table('rule')->find($v);
          if(isset($zi['rule_name'])){
            $lan[$parent['rule_name']][]=$zi['rule_name'];
          }
        }
      }
      $data['lan']=$lan;
      // dump($lan);
      $data['addtime']=date('Y-m-d H:i:s',$data['addtime']);
      $this->assign('data',$data);
      return $this->fetch();
    }
   

    public function index(){
      return $this->fetch();
    }
    public function lst(){
      $req=input("post.");
      $page=$req['page'];  //当前页   1
      $limit=$req['limit']; //每页几行数据  10
      $begin=($page-1)*$limit;   //0,10  10,10 
      $data=[];
      $list=Db::table('role')

          ->limit($begin,$limit)

          ->select();
      for ($i=0; $i <count($list) ; $i++) { 
        $list[$i]['addtime']=date("Y-m-d",$list[$i]['addtime']);
      }
      $count=Db::table('role')->count();
      $data['code']=0;
      $data['data']=$list;
      $data['msg']="返回成功";
      $data['count']=$count;
      echo json_encode($data);
    }
  public function add(){
      $lan=Db::table('rule')->where('pid','0')->order('sort asc')->select();
      for ($i=0; $i <count($lan) ; $i++) { 
        $zilan=Db::table('rule')->where('pid',$lan[$i]['rule_id'])->order('sort asc')->select();
        $lan[$i]['zilan']=$zilan;
        for ($j=0; $j <count($zilan) ; $j++) { 
           $quan=Db::table('rule')->where('pid',$zilan[$j]['rule_id'])->select();
           $lan[$i]['zilan'][$j]['quan']=$quan;
        }
        
      }
      $this->assign('lan',$lan);
      // dump($lan);
      return $this->fetch();
    }
    public function do_add(){
      $req=input('post.');
      $desc=$req['desc'];
      $lan=[];   //将大栏目放进数组再转成json
      $zilan=[];  //将小栏目设置成 ['lanid1'=>[],'lanid2'=>[]]的格式,再转成json
      if(isset($req['lan'])){
        foreach($req['lan'] as $k=>$v){
           $lan[]=$v['id'];
           $zilan[$v['id']]=[];
           if(isset($v['zilan'])){
            foreach($v['zilan'] as $key=>$vo){
              $zilan[$v['id']][]=$vo['id'];
             }
           }
           
        }
      }

      $count=Db::table('role')->where('name',$req['name'])->count();
      if($count>0){
        return 2;
      }
      $data=[];
      $data['addtime']=time();
      $data['name']=$req['name'];
      $data['desc']=$req['desc'];
      $data['lan']=json_encode($lan);
      $data['zilan']=json_encode($zilan);

      $num=Db::table('role')->insert($data);
      return $num;
    }
    public function update(){
      $lan=Db::table('rule')->where('pid','0')->order('sort asc')->select();
      for ($i=0; $i <count($lan) ; $i++) { 
        $zilan=Db::table('rule')->where('pid',$lan[$i]['rule_id'])->order('sort asc')->select();
        $lan[$i]['zilan']=$zilan;
        for ($j=0; $j <count($zilan) ; $j++) { 
           $quan=Db::table('rule')->where('pid',$zilan[$j]['rule_id'])->select();
           $lan[$i]['zilan'][$j]['quan']=$quan;
        }
        
      }
      // dump($lan);
      $this->assign('lan',$lan);
      $id=input('get.id');
      $data=Db::table('role')->find($id);
      // $data['lan']=json_decode($data['lan'],true);
      // dump($data['zilan']);
      $data['zilan']=json_decode($data['zilan'],true);
      // foreach($data['zilan'] as $k=>$v){
      //   if(is_array($v)){
      //     $data['zilan'][$k]['quan']=$v;
      //   }
      // }
      // dump($data['zilan']);
      $this->assign('data',$data);
      return $this->fetch();
    }
    public function do_update(){
      $id=input('get.id');
      $req=input('post.');
      $desc=$req['desc'];
      $lan=[];   //将大栏目放进数组再转成json
      $zilan=[];  //将小栏目设置成 ['lanid1'=>[],'lanid2'=>[]]的格式,再转成json
      if(isset($req['lan'])){
        foreach($req['lan'] as $k=>$v){
           $lan[]=$v['id'];
           $zilan[$v['id']]=[];
           if(isset($v['zilan'])){
              foreach($v['zilan'] as $key=>$vo){
              $zilan[$v['id']][]=$vo['id'];
                if(isset($vo['quan'])){
                  foreach($vo['quan'] as $ke=>$ve){
                  $zilan[$v['id']][]=$ve['id'];
                }
              }
             }
           }
           
        }
      }
      // dump($req);
      // dump($zilan);die;
      $count=Db::table('role')->where('name',$req['name'])->where('id',"<>",$id)->count();
      if($count>0){
        return 2;
      }
      $data=[];
      $data['name']=$req['name'];
      $data['desc']=$req['desc'];
      $data['lan']=json_encode($lan);
      $data['zilan']=json_encode($zilan);
      $num=Db::table('role')->where('id',$id)->update($data);
      return $num;
    }

    public function do_del(){   //下面有员工不能删除
      $data=input("post.");
      $count=Db::table('admin')->where('juese',$data['id'][0])->count();
      if($count>0){
        return 2;
      }
      $num=Db::table("role")->where("id",$data['id'][0])->delete();
      return $num;  
    }
    public function up_status(){
       $id=input("get.id");
       $data=Db::table('role')->where('id',$id)->find();
       $status=$data['status'];
       if($status==1){  //当前启用
          $num=Db::table('role')->where('id',$id)->update(['status'=>2]);
          if($num>0){
               return 1;
          }else{
               return 2;
          }
      }else{
          $num=Db::table('role')->where('id',$id)->update(['status'=>1]);
          if($num>0){
               return 3;
          }else{
               return 4;
          }
       }
    }
    //文件上传
    public function upload(){
    	$return=[];
    	 	// 获取表单上传文件 例如上传了001.jpg
		    $file = request()->file('image');
		    // 移动到框架应用根目录/public/uploads/ 目录下
		    if($file){
		        $info = $file->move(ROOT_PATH . 'public' . DS . 'uploads'.DS.'user');
		        if($info){
		            // 成功上传后 获取上传信息
					$return['code']=0;
				    $return['msg']="上传成功";
				    $return['data']=$info->getSaveName();
		        }else{
		            // 上传失败获取错误信息
		            $return['code']=1;
					// $return['msg']="上传成功";
				    $return['msg']="上传失败";
				    $return['data']=$file->getError();
		        }
		    }
	    return json_encode($return);
	}
   

 


    //图片验证码
        public function yan1()
    { 
      
      //1.创建黑色画布
$image = imagecreatetruecolor(100, 30);
 
//2.为画布定义(背景)颜色
$bgcolor = imagecolorallocate($image, 255, 255, 255);
 
//3.填充颜色
imagefill($image, 0, 0, $bgcolor);
 
// 4.设置验证码内容
 
//4.1 定义验证码的内容
$content = "0123456789";
 
//4.1 创建一个变量存储产生的验证码数据，便于用户提交核对
$captcha = "";
for ($i = 0; $i < 4; $i++) {
    // 字体大小
    $fontsize = 100;
    // 字体颜色
    $fontcolor = imagecolorallocate($image, mt_rand(0, 120), mt_rand(0, 120), mt_rand(0, 120));
    // 设置字体内容
    $fontcontent = substr($content, mt_rand(0, strlen($content)), 1);
    $captcha .= $fontcontent;
    // 显示的坐标
    $x = ($i * 100 / 4) + mt_rand(5, 10);
    $y = mt_rand(5, 10);
    // 填充内容到画布中
    imagestring($image, $fontsize, $x, $y, $fontcontent, $fontcolor);
}
// $_SESSION["captcha"] = $captcha;
 session("usercaptcha",$captcha);
//4.3 设置背景干扰元素
for ($$i = 0; $i < 200; $i++) {
    $pointcolor = imagecolorallocate($image, mt_rand(50, 200), mt_rand(50, 200), mt_rand(50, 200));
    imagesetpixel($image, mt_rand(1, 99), mt_rand(1, 29), $pointcolor);
}
 
//4.4 设置干扰线
for ($i = 0; $i < 3; $i++) {
    $linecolor = imagecolorallocate($image, mt_rand(50, 200), mt_rand(50, 200), mt_rand(50, 200));
    imageline($image, mt_rand(1, 99), mt_rand(1, 29), mt_rand(1, 99), mt_rand(1, 29), $linecolor);
}
 
//5.向浏览器输出图片头信息
header('content-type:image/png');
 
//6.输出图片到浏览器
imagepng($image);
 
//7.销毁图片
imagedestroy($image);
    }

}